package com.alibaba.otter.manager.web.resource;

import com.alibaba.otter.manager.biz.config.pipeline.PipelineService;
import com.alibaba.otter.manager.web.dto.PipelineInfoDTO;
import com.alibaba.otter.manager.web.dto.RestResponse;
import com.alibaba.otter.manager.web.service.PipelineInfoAggService;
import com.alibaba.otter.manager.web.utils.PaginationUtil;
import com.alibaba.otter.shared.common.model.config.pipeline.Pipeline;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 管道
 */
@RequestMapping("/api/v1/pipeline")
@RestController
public class PipelineResource {


    private final PipelineService pipelineService;


    private final PipelineInfoAggService pipelineInfoAggService;

    public PipelineResource(PipelineService pipelineService, PipelineInfoAggService pipelineInfoAggService) {
        this.pipelineService = pipelineService;
        this.pipelineInfoAggService = pipelineInfoAggService;
    }


    @GetMapping("findOne/{id}")
    public RestResponse<Pipeline> findOne(@PathVariable("id") Long id){
        return RestResponse.ok(pipelineService.findById(id));
    }


    @GetMapping("list")
    public RestResponse<List<PipelineInfoDTO>> list(@RequestParam("channelId") Long channelId,
                                                    @RequestParam(value = "pipelineId",required = false) Long pipelineId,
                                                    Pageable pageable){
        List<PipelineInfoDTO> pipelineInfo = pipelineInfoAggService.pipelineInfo(channelId, pipelineId);
        return PaginationUtil.page(pageable,pipelineInfo.size(),pipelineInfo);
    }

    @PostMapping("create")
    public RestResponse<Void> create(@RequestBody @Validated Pipeline node){
        pipelineInfoAggService.create(node);
        return RestResponse.ok();
    }


    @PutMapping("modify")
    public RestResponse<Void> modify(@RequestBody @Validated Pipeline node){
        pipelineInfoAggService.modify(node);
        return RestResponse.ok();
    }


    @DeleteMapping("delete/{id}")
    public RestResponse<Void> delete(@PathVariable("id") Long id){
        pipelineService.remove(id);
        return RestResponse.ok();
    }

}
